package com.quanshu.change.view;

import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.entity.AccountBalanceEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.shop.base.view.BaseTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoldWithdrawActivity extends BaseActivity {

    @BindView(R.id.title)
    BaseTitleBar title;
    @BindView(R.id.txt1)
    TextView txt1;
    @BindView(R.id.ed1)
    EditText ed1;
    @BindView(R.id.txt12)
    TextView txt12;
    @BindView(R.id.money_tips)
    TextView moneyTips;
    @BindView(R.id.cl1)
    ConstraintLayout cl1;
    @BindView(R.id.txt2)
    TextView txt2;
    @BindView(R.id.ed2)
    EditText ed2;
    @BindView(R.id.cl2)
    ConstraintLayout cl2;
    @BindView(R.id.txt3)
    TextView txt3;
    @BindView(R.id.ed3)
    EditText ed3;
    @BindView(R.id.cl3)
    ConstraintLayout cl3;
    @BindView(R.id.txt4)
    TextView txt4;
    @BindView(R.id.ed4)
    EditText ed4;
    @BindView(R.id.cl4)
    ConstraintLayout cl4;
    @BindView(R.id.txt5)
    TextView txt5;
    @BindView(R.id.ed5)
    EditText ed5;
    @BindView(R.id.cl5)
    ConstraintLayout cl5;
    @BindView(R.id.txt6)
    TextView txt6;
    @BindView(R.id.ed6)
    EditText ed6;
    @BindView(R.id.cl6)
    ConstraintLayout cl6;
    @BindView(R.id.txt7)
    TextView txt7;
    @BindView(R.id.ed7)
    EditText ed7;
    @BindView(R.id.cl7)
    ConstraintLayout cl7;
    @BindView(R.id.cl)
    ConstraintLayout cl;
    @BindView(R.id.txt_ok)
    TextView txtOk;

    private int money = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gold_withdraw);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        ImmersionBar.with(this).statusBarColor("#FFFFFF").titleBar(title).statusBarDarkFont(true).init();
        getBalance();
        title.setCenterText("提现").setLeftImage(R.drawable.icon_back_black_2_0).setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });

        RxViewHelper.getInstance().clicks(txtOk, aVoid -> {
            //提现
            doWithdraw();
        });

        ed1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    int a = Integer.parseInt(s.toString()) % 100;
                    if (a > 0) {
                        moneyTips.setText("提现金额需为100的整数倍");
                        moneyTips.setVisibility(View.VISIBLE);
                    } else {
                        moneyTips.setVisibility(View.GONE);
                    }
                    if (Integer.parseInt(s.toString()) > money) {
                        int b = money / 100;
                        ed1.setText("" + b * 100);
                        ed1.setSelection(ed1.length());
                    }
                } else {
                    moneyTips.setText("提现金额需不能为空");
                    moneyTips.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        ed4.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    //提现操作
    private void doWithdraw() {
        if (TextUtils.isEmpty(ed1.getText())) {
            ToastHelper.toast("请输入提现金额");
            return;
        }
        if (TextUtils.isEmpty(ed3.getText())) {
            ToastHelper.toast("请输入持卡人姓名");
            return;
        }
        if (ed3.getText().length() < 2) {
            ToastHelper.toast("收款人格式不正确");
            return;
        }
        if (TextUtils.isEmpty(ed4.getText())) {
            ToastHelper.toast("请输入银行卡号");
            return;
        }
        if (TextUtils.isEmpty(ed5.getText())) {
            ToastHelper.toast("请输入开户行");
            return;
        }
        if (TextUtils.isEmpty(ed6.getText())) {
            ToastHelper.toast("请输入开户行支行");
            return;
        }
        if (TextUtils.isEmpty(ed7.getText())) {
            ToastHelper.toast("请输入六位支付密码");
            return;
        }
        if (currentUser != null) {
            NetClient.getInstance().getShopService(ShopInterface.class)
                    .doWithdraw(currentUser.getUser_id() + "", ed1.getText().toString(), ed3.getText().toString(), ed4.getText().toString(), ed5.getText().toString(), ed6.getText().toString(), ed7.getText().toString())
                    .compose(new BaseTransformer<>(this, true, false))
                    .subscribe(new BaseObserver<BaseResponse<String>>() {
                        @Override
                        public void onSuccess(BaseResponse<String> response) {
                            if (response.getCode() == 1) {
                                ToastHelper.toast(response.getMsg());
                                finish();
                            }
                        }

                        @Override
                        public void onFailure(Throwable e) {
                            super.onFailure(e);
                            LogUtil.printException(e);
                        }
                    });
        }
    }

    //获取账户余额
    private void getBalance() {
        if (currentUser != null) {
            NetClient.getInstance().getShopService(ShopInterface.class)
                    .getBalance(currentUser.getMemberid())
                    .compose(new BaseTransformer<>(this, true, false))
                    .subscribe(new BaseObserver<BaseResponse<AccountBalanceEntity>>() {
                        @Override
                        public void onSuccess(BaseResponse<AccountBalanceEntity> response) {
                            if (response.getCode() == 1) {
                                currentUser.setScore(response.getData().getScore());
                                currentUser.setGp(response.getData().getGP());
                                MyApplication.getInstance().setCurrentUser(currentUser);
                                txt12.setText("可提现余额：" + String.valueOf(response.getData().getGold_bean()));
                                money = (int) response.getData().getGold_bean();

                            }
                        }

                        @Override
                        public void onFailure(Throwable e) {
                            LogUtil.printException(e);
                        }
                    });
        }
    }
}
